package com.simulation.web.controller.common;

import com.simulation.common.core.domain.AjaxResult;
import com.simulation.web.entity.OssFile;
import com.simulation.web.service.PostService;
import com.simulation.web.service.impl.MinioTemplate;
import com.simulation.web.vo.TechPostVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 唐雄杰
 * @Date 2024-07-31 22:35
 **/
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private MinioTemplate minioTemplate;

    @Resource
    private PostService postService;


    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("上传单个文件")
    public AjaxResult upload(@RequestPart("file") MultipartFile file) {
        OssFile ossFile = minioTemplate.upLoadFile("template", "projectTemplate", file);
        return AjaxResult.success(ossFile);
    }


    @GetMapping(value = "/download-by-projectId")
    @ApiOperation("下载单个文件")
    public void downloadByProjectId(@RequestParam("projectId") Long projectId, HttpServletResponse response) {
        TechPostVO techPostVO = postService.getById(projectId);
        minioTemplate.downloadFile(response, techPostVO.getTitle()+".zip", techPostVO.getCode());
    }
}
